package com.atty.dms.service.impl;

import com.atty.dms.common.convention.exception.ClientException;
import com.atty.dms.dao.entity.UserInterestDO;
import com.atty.dms.dao.mapper.UserInterestMapper;
import com.atty.dms.dto.req.BatchUserInterestCreateReqDTO;
import com.atty.dms.dto.req.SingleUserInterestUpdateReqDTO;
import com.atty.dms.service.UserInterestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class UserInterestServiceImpl extends ServiceImpl<UserInterestMapper, UserInterestDO> implements UserInterestService {

    private final UserInterestMapper userInterestMapper;


    /**
     * 根据用户ID获取兴趣名称列表
     */
    @Override
    public List<String> getInterestNamesByUserId(Long userId) {
        // 直接调用 Mapper 方法完成查询
        return userInterestMapper.getInterestNamesByUserId(userId);
    }

    @Override
    public void createUserInterest(Long userId, Long interestId) {
        // 参数校验
        if (userId == null || interestId == null) {
            throw new ClientException("用户ID和兴趣ID不能为空");
        }

        try {
            // 调用 Mapper 插入单条数据
            getBaseMapper().createUserInterest(userId, interestId);
        } catch (DataIntegrityViolationException e) {
            // 判断是否是用户ID不存在
            if (e.getMessage().contains("foreign key constraint")) {
                throw new ClientException("用户ID不存在：" + userId);
            }
            // 判断是否是兴趣已添加
            if (e.getMessage().contains("unique constraint")) {
                throw new ClientException("兴趣已添加：用户ID=" + userId + ", 兴趣ID=" + interestId);
            }
            // 其他数据完整性异常
            throw new ClientException("插入单条数据失败：" + e.getMessage());
        } catch (Exception e) {
            // 记录日志
            log.error("插入单条数据失败", e);
            throw new ClientException("插入单条数据失败");
        }
    }

    @Override
    public void batchCreateUserInterests(BatchUserInterestCreateReqDTO requestParam) {
        // 参数校验
        if (requestParam == null || requestParam.getUserId() == null || requestParam.getInterestIds() == null || requestParam.getInterestIds().isEmpty()) {
            throw new ClientException("请求参数无效：用户ID或兴趣ID列表不能为空");
        }

        Long userId = requestParam.getUserId();
        List<Long> interestIds = requestParam.getInterestIds();

        try {
            // 调用 Mapper 批量插入
            getBaseMapper().batchCreateUserInterests(userId, interestIds);
        } catch (DataIntegrityViolationException e) {
            // 判断是否是用户ID不存在
            if (e.getMessage().contains("foreign key constraint")) {
                throw new ClientException("用户ID不存在：" + userId);
            }
            // 判断是否是兴趣已添加
            if (e.getMessage().contains("unique constraint")) {
                throw new ClientException("部分兴趣已添加：用户ID=" + userId + ", 兴趣ID列表=" + interestIds);
            }
            // 其他数据完整性异常
            throw new ClientException("批量插入数据失败：" + e.getMessage());
        } catch (Exception e) {
            // 记录日志
            log.error("批量插入数据失败", e);
            throw new ClientException("批量插入数据失败");
        }
    }


    public void deleteUserInterest(Long userId, Long interestId) {
        // 参数校验
        if (userId == null || interestId == null) {
            throw new ClientException("用户ID和兴趣ID不能为空");
        }
        try {
            // 调用 Mapper 删除数据
            getBaseMapper().deleteUserInterest(userId, interestId);
        } catch (Exception e) {
            // 记录日志
            log.error("删除数据失败", e);
            throw new ClientException("删除数据失败");
        }
    }

    @Override
    public void deleteUserInterests(BatchUserInterestCreateReqDTO requestParam) {
        if(requestParam == null || requestParam.getUserId() == null || requestParam.getInterestIds() == null || requestParam.getInterestIds().isEmpty()){
            throw new ClientException("请求参数无效：用户ID或兴趣ID列表不能为空");
        }
        try {
            // 调用 Mapper 批量删除
            getBaseMapper().deleteUserInterests(requestParam.getUserId(), requestParam.getInterestIds());
        } catch (Exception e) {
            // 记录日志
            log.error("批量删除数据失败", e);
            throw new ClientException("批量删除数据失败");
        }
    }

    @Override
    public void updateUserInterest(SingleUserInterestUpdateReqDTO requestParam) {
        if (requestParam == null || requestParam.getUserId() == null || requestParam.getNewInterestId() == null){
            throw new ClientException("请求参数无效：用户ID或兴趣ID不能为空");
        }
        try {
            // 调用 Mapper 更新数据
            getBaseMapper().updateUserInterest(requestParam.getUserId(), requestParam.getOldInterestId(), requestParam.getNewInterestId());
        } catch (Exception e) {
            // 记录日志
            log.error("更新数据失败", e);
            throw new ClientException("更新数据失败");
        }
    }

    @Override
    public List<String> getUsernamesByInterestId(Long interestId) {
        if (interestId == null) {
            throw new IllegalArgumentException("兴趣ID不能为空");
        }
        try {
           return userInterestMapper.getUsernamesByInterestId(interestId);
        }catch (Exception e){
            log.error("根据兴趣id查询用户名失败", e);
            throw new ClientException("根据兴趣id查询用户名失败");
        }
    }

}